/**
 * 数字+字母键盘 @author by luys
 * 页面引入该js文件即可
 * 
 **/
//初始化软键盘
function initialKeyboard(){
	var numberItem=['1','2','3','4','5','6','7','8','9','0','.','Del'];
	var enUpItem=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','-','/'];
	var enLowItem=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','-','/'];
	var numberKeyboard="<div id='numberKeyboard' style='z-index:2000;display:none;width:546px;height:35px;position:absolute;bottom:60px;left: 50%;transform: translateX(-50%);-moz-border-radius: 5px;-webkit-border-radius: 5px;border-radius: 5px;border:1px solid #01FFCC;background-color:white;filter:Alpha(opacity=0.9);opacity:0.9;-moz-user-select: none;-khtml-user-select: none;user-select: none;'>" ;
	for(var i=0;i<numberItem.length;i++){
		numberKeyboard+="<div style='width:35px;height:31px;margin:2px;float:left;-moz-border-radius: 2px;-webkit-border-radius: 2px;border-radius: 2px;background-color:#01D8FF;font-size:20px;font-family:serif;font-weight:600;color:white;line-height: 31px;text-align: center;' KeyboardItemValue='"+numberItem[i]+"'>"+numberItem[i]+"</div>" ;
	}
	numberKeyboard+="<div id='keyboard_type' style='width:35px;height:31px;margin:2px;float:left;-moz-border-radius: 2px;-webkit-border-radius: 2px;border-radius: 2px;background-color:#01D8FF;font-size:20px;font-family:serif;font-weight:600;color:white;line-height: 31px;text-align: center;' KeyboardItemValue='changeType' changeType='Num'>Num</div>" ;
	numberKeyboard+="<div style='width:35px;height:31px;margin:2px;float:left;-moz-border-radius: 2px;-webkit-border-radius: 2px;border-radius: 2px;background-color:#01D8FF;font-size:20px;font-family:serif;font-weight:600;color:white;line-height: 31px;text-align: center;' KeyboardItemValue='closed'>关</div>" ;
	numberKeyboard+="</div>";
	
	var enLowKeyboard="<div id='enLowKeyboard' style='z-index:2000;display:none;width:546px;height:70px;position:absolute;bottom:95px;left: 50%;transform: translateX(-50%);-moz-border-radius: 5px;-webkit-border-radius: 5px;border-radius: 5px;border:1px solid #81B8FB;background-color:white;filter:Alpha(opacity=0.9);opacity:0.9;-moz-user-select: none;-khtml-user-select: none;user-select: none;'>";
	for(var i=0;i<enLowItem.length;i++){
		enLowKeyboard+="<div style='width:35px;height:31px;margin:2px;float:left;-moz-border-radius: 2px;-webkit-border-radius: 2px;border-radius: 2px;background-color:#147BFA;font-size:20px;font-family:serif;font-weight:600;color:white;line-height: 31px;text-align: center;' KeyboardItemValue='"+enLowItem[i]+"'>"+enLowItem[i]+"</div>" ;
	}
	enLowKeyboard+="</div>";
	
	var enUpKeyboard="<div id='enUpKeyboard' style='z-index:2000;display:none;width:546px;height:70px;position:absolute;bottom:95px;left: 50%;transform: translateX(-50%);-moz-border-radius: 5px;-webkit-border-radius: 5px;border-radius: 5px;border:1px solid #81B8FB;background-color:white;filter:Alpha(opacity=0.9);opacity:0.9;-moz-user-select: none;-khtml-user-select: none;user-select: none;'>";
	for(var i=0;i<enUpItem.length;i++){
		enUpKeyboard+="<div style='width:35px;height:31px;margin:2px;float:left;-moz-border-radius: 2px;-webkit-border-radius: 2px;border-radius: 2px;background-color:#147BFA;font-size:20px;font-family:serif;font-weight:600;color:white;line-height: 31px;text-align: center;' KeyboardItemValue='"+enUpItem[i]+"'>"+enUpItem[i]+"</div>" ;
	}
	enUpKeyboard+="</div>";
	
	
	//在顶层页面加载软键盘
	$(window.top.document.body).append(numberKeyboard);
	$(window.top.document.body).append(enLowKeyboard);
	$(window.top.document.body).append(enUpKeyboard);
}

//缓存当前获取焦点的对象(MiniUI对象)
var onfocusItem_tmp;

$(function(){
	//判断是否已经加载软键盘，如果没有则加载
	if($("#numberKeyboard",window.top.document).length==0){
		initialKeyboard();
		//加载软键盘点击事件（所有有属性KeyboardItemValue的DIV）
		$("[KeyboardItemValue]",window.top.document).on("click",function(){
			var val=$(this).attr("KeyboardItemValue");//当前点击的软键盘的值
			if(val=="closed"){
				$("#numberKeyboard",window.top.document).hide();
				$("#enLowKeyboard",window.top.document).hide();
				$("#enUpKeyboard",window.top.document).hide();
			}else if(val=="changeType"){
				if($(this).attr("changeType")=="Num"){
					$(this).css("color","red");
					$(this).attr("changeType","en");
					$(this).html("en");
					$("#enLowKeyboard",window.top.document).show();
					$("#enUpKeyboard",window.top.document).hide();
				}else if($(this).attr("changeType")=="en"){
					$(this).css("color","red");
					$(this).attr("changeType","EN");
					$(this).html("EN");
					$("#enLowKeyboard",window.top.document).hide();
					$("#enUpKeyboard",window.top.document).show();
				}else if($(this).attr("changeType")=="EN"){
					$(this).css("color","white");
					$(this).attr("changeType","Num");
					$(this).html("Num");
					$("#enLowKeyboard",window.top.document).hide();
					$("#enUpKeyboard",window.top.document).hide();
				}
			}else{
				//获取当前焦点的缓存对象，因为软键盘在父页面中，而缓存对象在子页面中，因此要特殊处理
				for(var i=0;i<$("iframe").length;i++){
					//先从顶级页面下的iframe中找（因为顶级页面下可能不止一个ifram，所以外面要嵌套循环）
					var onfocus_item=$("iframe")[i].contentWindow.onfocusItem_tmp;
					if(onfocus_item==undefined){//如果顶层页面下的iframe没有找到，则看iframe下面还有没有下一级iframe
						var secChildIframe=$("iframe").contents().find("iframe")[0];
						if(secChildIframe!=undefined){//如果有则找下一级iframe中的页面
							onfocus_item=secChildIframe.contentWindow.onfocusItem_tmp;
						}
					}
					if(onfocus_item!=undefined){
						onfocus_item.focus();
						var orignal_val=onfocus_item.getValue();
						var cursorPosition=getCursorPosition($("iframe")[i].contentWindow.document,onfocus_item.getTextEl());//当前输入框光标位置
						if(val!="Del"){
							onfocus_item.setValue(orignal_val.substring(0,cursorPosition)+val+orignal_val.substring(cursorPosition,orignal_val.length));
							setCursorPosition(onfocus_item.getTextEl(),cursorPosition+1);
						}else{
							onfocus_item.setValue(orignal_val.substring(0,cursorPosition-1)+orignal_val.substring(cursorPosition,orignal_val.length));
							setCursorPosition(onfocus_item.getTextEl(),cursorPosition-1);
						}
						onfocus_item.doValueChanged();
					}
				};
			}
		});
	}
	
	
	//输入框点击事件
	$(".mini-textbox").on("click",function(){
		//缓存当前输入框
		var id=$(this)[0].id;
		if(id==""){
			id=$(this)[0].uid;//根据uid获取miniui对象，因为有些没有指定ID，所以只能根据ID获取对象
		}
		onfocusItem_tmp=mini.get(id);
		//顶层页面显示软键盘
		$("#numberKeyboard",window.top.document).show();
		var type=$("#keyboard_type",window.top.document).attr("changeType");
		if(type=="en"){
			$("#enLowKeyboard",window.top.document).show();
		}else if(type=="EN"){
			$("#enUpKeyboard",window.top.document).show();
		}
	});
}); 

//获取光标位置
function getCursorPosition(document_,dom){
	var cursurPosition=-1;
	if(dom.selectionStart!=undefined){//非IE浏览器
		cursurPosition= dom.selectionStart;
	}else{//IE
		var range = document_.selection.createRange();
		range.moveStart("character",-dom.value.length);
		cursurPosition=range.text.length;
	}
	return cursurPosition;
}
//设置光标位置
function setCursorPosition(dom,cursorPosition){
    if(dom.selectionStart!=undefined){//非IE浏览器
    	dom.selectionStart=cursorPosition;
    	dom.selectionEnd=cursorPosition;
    }else{//IE
        var range = dom.createTextRange();
        range.move("character",cursorPosition);
        range.select();
    }
}














/**系统软键盘，此功能作废**/
function openKeyboard(){
	$.ajax({
        url: ctx+"/system/openKeyboard.do",
        type: "post",
        success:function(text){
        	if(!text){
        		mini.alert("打开系统软键盘失败！");
        	}
        }
    })
}
function closeKeyboard(){
	$.ajax({
        url: ctx+"/system/closeKeyboard.do",
        type: "post",
        success:function(text){
        }
    })
}